package com.wondersgroup.framework.widget.taglib.window;

import com.wondersgroup.framework.widget.annotations.WidgetTag;
import com.wondersgroup.framework.widget.annotations.WidgetTagAttribute;
import com.wondersgroup.framework.widget.library.extjs.ExtComponent;
import com.wondersgroup.framework.widget.library.extjs.PlainTextComponent;
import com.wondersgroup.framework.widget.library.extjs.window.ExtWindow;
import com.wondersgroup.framework.widget.taglib.WidgetTagSupport;
import com.wondersgroup.framework.widget.util.OutputUtils;
import com.wondersgroup.framework.widget.util.TagUtils;
import com.wondersgroup.framework.widget.validation.annotation.TypeValidate;

import javax.servlet.jsp.JspException;

import org.springframework.beans.BeanUtils;

@WidgetTag(name = "window", tldTagClass = "com.wondersgroup.framework.widget.taglib.window.WindowTag", description = "Render an Ext.Window")
public class WindowTag extends WidgetTagSupport {
	private String title = "弹出窗口";
	private String url;
	private int width = 400;
	private int height = 300;

	@TypeValidate
	private boolean modal;

	@TypeValidate
	private boolean minimizable;

	@TypeValidate
	private boolean maximizable;

	@TypeValidate
	private boolean autoScroll;

	public String getTitle() {
		return this.title;
	}

	public String getUrl() {
		return this.url;
	}

	public int getWidth() {
		return this.width;
	}

	public int getHeight() {
		return this.height;
	}

	public boolean isModal() {
		return this.modal;
	}

	public boolean isMinimizable() {
		return this.minimizable;
	}

	public boolean isMaximizable() {
		return this.maximizable;
	}

	@WidgetTagAttribute(description = "id", required = true, type = "String")
	public void setId(String paramString) {
		this.id = paramString;
	}

	@WidgetTagAttribute(description = "标题，默认值为：弹出窗口", required = false, type = "String")
	public void setTitle(String paramString) {
		this.title = paramString;
	}

	@WidgetTagAttribute(description = "弹出窗口URL", required = false, type = "String")
	public void setUrl(String paramString) {
		this.url = paramString;
	}

	@WidgetTagAttribute(description = "弹出窗口宽度，默认值为400", required = false, type = "int")
	public void setWidth(int paramInt) {
		this.width = paramInt;
	}

	@WidgetTagAttribute(description = "弹出窗口高度，默认值为300", required = false, type = "int")
	public void setHeight(int paramInt) {
		this.height = paramInt;
	}

	@WidgetTagAttribute(description = "弹出窗口模式", required = false, type = "boolean")
	public void setModal(boolean paramBoolean) {
		this.modal = paramBoolean;
	}

	@WidgetTagAttribute(description = "弹出窗口最小化标志", required = false, type = "boolean")
	public void setMinimizable(boolean paramBoolean) {
		this.minimizable = paramBoolean;
	}

	@WidgetTagAttribute(description = "弹出窗口最大化标志", required = false, type = "boolean")
	public void setMaximizable(boolean paramBoolean) {
		this.maximizable = paramBoolean;
	}

	public boolean isAutoScroll() {
		return this.autoScroll;
	}

	@WidgetTagAttribute(type = "boolean", defaultValue = "false", description = "[可选]自动设定滚动条，默认为false")
	public void setAutoScroll(boolean paramBoolean) {
		this.autoScroll = paramBoolean;
	}

	public void processEndTagContent() throws JspException {
		StringBuffer localStringBuffer = new StringBuffer();
		ExtWindow localExtWindow = new ExtWindow();
		BeanUtils.copyProperties(this, localExtWindow);
		if (getUrl() != null) {
			localExtWindow.setHtml("html");
		} else {
			localStringBuffer.append("</div>");
			localExtWindow.setContentEl(getId() + "_div");
			localExtWindow.setCloseAction("hide");
		}
		localStringBuffer.append(OutputUtils.ExtOnReady(new ExtComponent[] { localExtWindow }));
		String localString = localStringBuffer.toString().replace("<iframe ", "<iframe frameborder=\"0\" ");
		TagUtils.getInstance().writePrevious(this.pageContext, localStringBuffer.toString().replace("<iframe ", "<iframe frameborder=\"0\" "));
	}

	public void processTagContent() throws JspException {
		StringBuffer localStringBuffer1 = new StringBuffer();
		if (getUrl() != null) {
			PlainTextComponent localPlainTextComponent = new PlainTextComponent();
			StringBuffer localStringBuffer2 = new StringBuffer();
			localStringBuffer2.append("var ").append(getId()).append("Win;");
			localStringBuffer2.append("var ").append(getId()).append(";");
			localPlainTextComponent.setContent(localStringBuffer2.toString());
			localStringBuffer1.append(OutputUtils.writeScript(new ExtComponent[] { localPlainTextComponent }));
		} else {
			localStringBuffer1.append("<div id='" + getId() + "_div' class=\"x-hidden\">");
		}
		TagUtils.getInstance().writePrevious(this.pageContext, localStringBuffer1.toString());
	}
}
